# vim: set noexpandtab tabstop=4 softtabstop=4 shiftwidth=4:

CC           = g++
CFLAGS       = -Wall -ggdb
LDFLAGS      =
TARGET       = cardoon
INCLUDES     = -I. -I./third-party/gtest-1.6.0/include/
SOURCES      = cardoon.cpp 
OBJS         = $(patsubst %.cpp,%.o,$(SOURCES))
TEST_SOURCES = base/bits_unittest.cpp 
TEST_OBJS    = $(patsubst %.cpp,%.o,$(TEST_SOURCES))

$(TARGET): $(OBJS)
	$(CC) $(OBJS) -o $(TARGET) $(LDFLAGS) -L./
	rm -rf *.o

$(OBJS): $(SOURCES)
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

.PHONY: all clean c rebuild r test test_clean

all: $(TARGET)

test: $(TEST_OBJS)
	@for i in $(TEST_OBJS);                                                              \
	do                                                                                   \
		target=$${i%\.o};                                                                \
		$(CC) $$i ./third-party/gtest-1.6.0/make/gtest_main.a -o $$target -lpthread;     \
		./$$target;                                                                      \
	done

test_clean:
	for i in $(TEST_OBJS);                                                               \
	do                                                                                   \
		target=$${i%\.o}; echo $$target;                                                 \
		rm -f ./$$target;                                                                \
	done


$(TEST_OBJS): $(TEST_SOURCES)
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

clean: test_clean
	rm -rf *.o
	rm -rf base/*.o
	rm -rf $(TARGET)

c: clean
rebuild: clean all
r: rebuild
